package com.hrsaas.ext.spider.client;

import java.util.Map.Entry;

import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;

/**
 * <p>
 * 功能：TODO
 * <p>
 * @author wangxiaoliang@kanzhun.com
 *
 * 2018年8月1日
 */
public class HeaderFactory {

	/**
	 * 拉钩header
	 */
	public static final HttpHeaders EASY_INDEX = new DefaultHttpHeaders(false)
			.add(HttpHeaderNames.ACCEPT.toString(), "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
			.add(HttpHeaderNames.ACCEPT_LANGUAGE.toString(),"en-US,en;q=0.5")
			.add(HttpHeaderNames.ACCEPT_ENCODING.toString(), "gzip, deflate, br")
			.add(HttpHeaderNames.CONNECTION.toString(),"close")
			.add("Upgrade-Insecure-Requests", "1");
	
	public static final HttpHeaders COLLECTIOINS = new DefaultHttpHeaders(false)
			.add(HttpHeaderNames.ACCEPT.toString(), "*/*")
			.add(HttpHeaderNames.ACCEPT_LANGUAGE.toString(),"en-US,en;q=0.5")
			.add(HttpHeaderNames.ACCEPT_ENCODING.toString(), "gzip, deflate, br")
			.add(HttpHeaderNames.CONNECTION.toString(), "close");
	
	public static final HttpHeaders LOGINJSON = new DefaultHttpHeaders(false)
			.add(HttpHeaderNames.ACCEPT.toString(), "application/json, text/javascript, */*; q=0.01")
			.add(HttpHeaderNames.ACCEPT_LANGUAGE.toString(),"en-US,en;q=0.5")
			.add(HttpHeaderNames.ACCEPT_ENCODING.toString(), "gzip, deflate")
			.add(HttpHeaderNames.CONTENT_TYPE.toString(), "application/x-www-form-urlencoded; charset=UTF-8")
			.add("X-Requested-With","XMLHttpRequest")
			.add(HttpHeaderNames.CONNECTION.toString(), "close");
	
	public static final HttpHeaders GRANT = new DefaultHttpHeaders(false)
			.add(HttpHeaderNames.ACCEPT.toString(), "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
			.add(HttpHeaderNames.ACCEPT_LANGUAGE.toString(),"en-US,en;q=0.5")
			.add(HttpHeaderNames.ACCEPT_ENCODING.toString(), "gzip, deflate, br")
			.add(HttpHeaderNames.CONNECTION.toString(), "close")
			.add("Upgrade-Insecure-Requests", "1");
	
	public static final HttpHeaders FRESH = new DefaultHttpHeaders(false)
			.add(HttpHeaderNames.ACCEPT.toString(), "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
			.add(HttpHeaderNames.ACCEPT_LANGUAGE.toString(),"en-US,en;q=0.5")
			.add(HttpHeaderNames.CONNECTION.toString(), "close")
			.add("Upgrade-Insecure-Requests", "1");	
	
	public static final String toString(HttpHeaders headers) {
		StringBuilder builder = new StringBuilder();
		for (Entry<String, String> entry : headers.entries()) {
			builder.append(entry.getKey()).append(":").append(entry.getValue()).append("#");
		}
		return builder.toString();
	}
	
	public static final HttpHeaders extract(String headers) {
		if (headers != null && headers.length() != 0) {
			String[] split = headers.split("#");
			HttpHeaders hs = new DefaultHttpHeaders(false);
			for (String header : split) {
				String[] entry = header.split(":");
				hs.add(entry[0], entry[1]);
			}
			return hs;
		}
		return HttpHeaders.EMPTY_HEADERS;
	}
}
